#include "OccEdgeSelector.h"
#include <qDebug.h>
#include <QMessageBox>

OccEdgeSelector::OccEdgeSelector()
{
}
OccEdgeSelector::~OccEdgeSelector() {

}

bool OccEdgeSelector::addSelectedShape(const TopoDS_Shape& shape) {
    auto copyShape = shape;

    if (shape.ShapeType() != TopAbs_EDGE) {
        qDebug() << "WARNING: trying to delete a non-Edge shape via EdgeSelector.";
        return false;
    }
    auto it = std::find_if(m_selectedEdges.begin(), m_selectedEdges.end(),
        [&shape](const TopoDS_Shape& ref) {
            return ref == shape;
        });
    if (it != m_selectedEdges.end()) {
        return false;
    }
    m_selectedEdges.push_back(copyShape);
    return true;
}

bool OccEdgeSelector::deleteSelectedShape(const TopoDS_Shape& shape) {
    // 查找 shape 是否存在于 m_selectedShapes 中
    auto it = std::find_if(m_selectedEdges.begin(), m_selectedEdges.end(),
        [&shape](const TopoDS_Shape& ref) {
            return ref == shape;
        });
    // 如果找到了 shape
    if (it != m_selectedEdges.end()) {
        // 从向量中删除 shape
        m_selectedEdges.erase(it);
        return true; // 表示成功删除
    }

    return true;
}


std::vector<TopoDS_Shape> OccEdgeSelector::getSelectedEdges() {
    return m_selectedEdges;
}

const TopoDS_Edge& OccEdgeSelector::getFirstSelected() {
    if (m_selectedEdges.empty()) {
        qDebug() << "[ERROR]: Getting shape from empty list.";
    }
    return TopoDS::Edge(m_selectedEdges[0]);
}


bool OccEdgeSelector::isDataValid() {
    if (m_selectedEdges.empty()) return false;
    for (auto item : m_selectedEdges) {
        if (item.ShapeType() != TopAbs_EDGE) return false;
    }
    return true;
}

void OccEdgeSelector::clearSelectedShape() {
    m_selectedEdges.clear();
    //TODO: 发送信号？
}

const TopoDS_Shape& OccEdgeSelector::getSelectedShape() {
    if (isDataValid()) {
        return m_selectedEdges[0];
    }
}
